﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Config
{
    public class TestPoint3DList
    {
        XmlNode _xmlNode = null;
        XmlDocument _xmlDoc = null;
        private List<TestPoint3D> _list = null;
        public TestPoint3DList(XmlNode Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
            _xmlNode = Node;
            _xmlDoc = _xmlNode.OwnerDocument;

        }
        public XmlNode XmlNode
        {
            get
            {
                return _xmlNode;
            }
        }
        public void AddTestPoint3D(int id)
        {
            try
            {
                XmlNode node = _xmlDoc.CreateElement("item");
                TestPoint3D ch = new TestPoint3D(node);
                ch.Initial();
                ch.No = id;
                _xmlNode.AppendChild(ch.Node);
            }
            catch (Exception ex)
            {

            }
        }

        public List<TestPoint3D> TestPoint3Ds
        {
            get
            {
                // List<BaseNode> lst = new List<BaseNode>();
                TestPoint3D c = null;

                if (null != _list)
                    _list.Clear();
                else
                    _list = new List<TestPoint3D>();

                foreach (XmlNode x in _xmlNode.SelectNodes("item"))
                {
                    c = new TestPoint3D(x);
                    //  BaseNode b = c as BaseNode;
                    _list.Add(c);
                }
                //lst.Sort(delegate(BaseNode x, BaseNode y) { return x.id.CompareTo(y.id); });
                //foreach (BaseNode b in lst)
                //{
                //    _list.Add(b as Cls_TestPoint3D);
                //}
                _list.Sort();
                return _list;
            }
        }
    }
    public class TestPoint3D:BaseNode
    {
        public TestPoint3D(XmlNode Node) : base(Node)
        {
            if (Node == null)
            {
                throw new XmlException("Node is equal null");
            }
        }
        public void Initial()
        {
            _xmlNode.Attributes.Append(_xmlDoc.CreateAttribute("NO")).Value = "";
            XmlNode node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("channelname"));
            node.InnerText = "";
            node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("enable"));
            node.InnerText = "";
        }
        //public XmlNode XmlNode
        //{
        //    get
        //    {
        //        return _xmlNode;
        //    }
        //}
        //public string Name
        //{
        //    get
        //    {
        //        return _xmlNode.Name;
        //    }
        //}
        //public int No
        //{
        //    get
        //    {
        //        if (_xmlNode.Attributes["NO"] == null)
        //        {
        //            return -1;
        //        }
        //        return int.Parse(_xmlNode.Attributes["NO"].Value);
        //    }
        //    set
        //    {
        //        if (_xmlNode.Attributes["NO"] == null)
        //        {
        //            XmlDocument doc = _xmlNode.OwnerDocument;
        //            _xmlNode.Attributes.Append(doc.CreateAttribute("NO")).Value = value.ToString();
        //        }
        //        else
        //            _xmlNode.Attributes["NO"].Value = value.ToString();
        //    }
        //}
        public string TestPoint3DName
        {
            get
            {
                if (_xmlNode == null)
                {
                    return "";
                }
                return _xmlNode["channelname"].InnerText;
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("channelname");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("channelname"));
                Node.InnerText = value.ToString();
            }
        }
        public int Enable
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1;
                }
                return int.Parse(_xmlNode["enable"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("enable");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("enable"));
                Node.InnerText = value.ToString();
            }
        }
        //<channelname>测点1</channelname>
        //<enable>1</enable>
        //<channelid>0</channelid>
        public int channelid
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1;
                }
                return int.Parse(_xmlNode["channelid"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("channelid");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("channelid"));
                Node.InnerText = value.ToString();
            }
        }
        //<channelindexx>0</channelindexx>
        public int channelindexx
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1;
                }
                return int.Parse(_xmlNode["channelindexx"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("channelindexx");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("channelindexx"));
                Node.InnerText = value.ToString();
            }
        }
        //<channelindexy>1</channelindexy>
        public int channelindexy
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1;
                }
                return int.Parse(_xmlNode["channelindexy"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("channelindexy");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("channelindexy"));
                Node.InnerText = value.ToString();
            }
        }
        //<channelindexz>2</channelindexz>
        public int channelindexz
        {
            get
            {
                if (_xmlNode == null)
                {
                    return -1;
                }
                return int.Parse(_xmlNode["channelindexz"].InnerText);
            }
            set
            {
                XmlNode Node = _xmlNode.SelectSingleNode("channelindexz");
                if (Node == null)
                    Node = _xmlNode.AppendChild(_xmlNode.OwnerDocument.CreateElement("channelindexz"));
                Node.InnerText = value.ToString();
            }
        }
    }
}
